package utils;

import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;


import utils.AiSearch.AiNode;
import utils.AiSearch.AiNodeComparator;

public class PriorityQueueNoDupes<E> extends PriorityQueue<E> {

	AiNodeComparator comparator;
	public PriorityQueueNoDupes(int i, Comparator aiNodeComparator) {
		super(i,aiNodeComparator);
		comparator = (AiNodeComparator) aiNodeComparator;
	}

	public boolean add(E e) {		
		boolean isAdded = false;
		if (!super.contains(e))
		{
			isAdded = super.add(e);
		}
		else
		{
			Iterator<E> it= iterator();
			AiNode node = (AiNode) e;
			while(it.hasNext())
			{
				AiNode next = (AiNode) it.next();
				if(next.equals(node))
				{
					if(comparator.compare(node, next)<0)
					{
						this.remove(next);
						return super.add(e);
					}
					else
						return false;
				}
			}
		}
		return isAdded;
	}
}
